/**
 * 
 */
package z.tool.service;

import java.util.Map;

import z.tool.util.ZUtils;

/**
 * @author auzll@163.com
 */
public class ServiceResult {
    private static final String DEFAULT_VALUE_KEY = "ServiceResultDefaultValue";
    
    private boolean success;
    private Throwable exception;
    private String desc;
    private Map<String, Object> results = null;
    
    public ServiceResult add(Class<?> clazz, Object value) {
        return add(clazz.getName(), value);
    }
    
    public ServiceResult add(String key, Object value) {
        if (null == results) {
            results = ZUtils.newHashMap();
        }
        results.put(key, value);
        return this;
    }
    
    public ServiceResult desc(String desc) {
        this.desc = desc;
        return this;
    }

    public ServiceResult exception(Throwable exception) {
        this.exception = exception;
        return this;
    }

    public String getDesc() {
        return desc;
    }

    public Throwable getException() {
        return exception;
    }

    public Map<String, Object> getResults() {
        return results;
    }

    @SuppressWarnings("unchecked")
    public <T> T getValue() {
        return (T) results.get(DEFAULT_VALUE_KEY);
    }

    @SuppressWarnings("unchecked")
    public <T> T getValue(String key) {
        return (T) results.get(key);
    }
    
    public boolean isSuccess() {
        return success;
    }
    
    public ServiceResult success(boolean success) {
        this.success = success;
        return this;
    }
    
    public ServiceResult value(Object value) {
        return add(DEFAULT_VALUE_KEY, value);
    }
}
